package com.crazecoder.flutter.iflyad.utils;

import android.app.Activity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.iflytek.voiceads.IFLYBannerAd;
import com.iflytek.voiceads.IFLYInterstitialAd;
import com.iflytek.voiceads.config.AdError;
import com.iflytek.voiceads.config.AdKeys;
import com.iflytek.voiceads.listener.IFLYAdListener;

import java.util.HashMap;

import io.flutter.Log;

public class InterstitialUtil implements IFLYAdListener {
    private static InterstitialUtil instance;
    private Activity activity;
    private IFLYInterstitialAd interstitialAd;

    private InterstitialUtil(Activity activity) {
        this.activity = activity;
    }

    public static InterstitialUtil getInstance(Activity activity) {
        if (instance == null) {
            instance = new InterstitialUtil(activity);
        }
        return instance;
    }

    public void show(String adUnitId) {
        //创建插屏广告：adUnitId：开发者在讯飞AI营销云平台(http://www.voiceads.cn/)申请的插屏广告位 ID
        interstitialAd =
                IFLYInterstitialAd.createInterstitialAd(activity, adUnitId);
        interstitialAd.setParameter(AdKeys.OAID, OAIDUtil.getInstance(activity).getOAID());
        LinearLayout content = new LinearLayout(activity);
        content.setOrientation(LinearLayout.VERTICAL);
        content.setGravity(Gravity.CENTER);
        content.removeAllViews();
        content.addView(interstitialAd);
        activity.addContentView(
                content,
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        interstitialAd.loadAd(this);
    }
    /**
     * 广告请求成功
     */
    @Override
    public void onAdReceive() {
        //展示广告
        interstitialAd.showAd();
        Log.d("Ad_Android_Demo", "request success");
    }

    /**
     * 获取广告失败
     */
    @Override
    public void onAdFailed(AdError error) {
        Log.d("Ad_Android_Demo", "request failed:" + error.getErrorCode() + "," + error.getErrorDescription());
    }

    /**
     * 广告被点击
     */
    @Override
    public void onAdClick() {
        Log.d("Ad_Android_Demo", "onAdClick");
    }

    /**
     * 广告被关闭
     */
    @Override
    public void onAdClose() {
        Log.d("Ad_Android_Demo", "onAdClose");
    }

    /**
     * 广告曝光
     */
    @Override
    public void onAdExposure() {
        Log.d("Ad_Android_Demo", "onAdExposure");
    }

    /**
     * 下载确认
     */
    @Override
    public void onConfirm() {
        Log.d("Ad_Android_Demo", "onConfirm");
    }

    /**
     * 下载取消
     */
    @Override
    public void onCancel() {
        Log.d("Ad_Android_Demo", "onCancel");
    }
}
